using UnityEngine;
using System.Collections;

public class badGuyClass : MonoBehaviour {
	
	public GameObject Target;	
	public OTSprite TargetOT;	
	public OTSprite freezeEffectSample;
	public globalVarsClass gv;
	public shieldClass sh;
	private OTSprite me;	
	
	public float MobCurrentSpeed = 0.5f;
	public float MobCurrentBlastSpeed = 1.5f;
	//private Vector3 dir;
	private float xMove;
	private float yMove;
	private float angle;
	public float healthMax = 100; // стартовое количество хп монстра
	public float health;
	
    public OTSprite bullet;     
	public float shootDelay = 0;  
	public float shootCD = 20;  //скорострельность монстров
	
	private float slowPower = 1.0f;// 1- no slow, 0 - stopped
	private float frozen = 0; // глубина заморозки от 0 - не заморожен до +бесконечности
	private OTSprite freezeEffect;	
	//private float slowTime = 0;
	

	//Инициализация
	void Awake () {	
        me = this.gameObject.GetComponent<OTSprite>();
	}
	void Start () {		
     //   me = this.gameObject.GetComponent<OTSprite>();
		gv = GameObject.Find("GlobalVariables").GetComponent<globalVarsClass>();
		sh = GameObject.Find("Shield").GetComponent<shieldClass>();
        TargetOT = Target.gameObject.GetComponent<OTSprite>();
		
		health = healthMax;
		HealthBar hb = GetComponent<HealthBar>();
        hb.health = (health);
        hb.healthMax = (healthMax);
		
		angle = Mathf.Atan2(transform.position.y - Target.transform.position.y,
			transform.position.x - Target.transform.position.x);
		yMove = MobCurrentSpeed*Mathf.Sin(angle);
		xMove = MobCurrentSpeed*Mathf.Cos(angle);
	}
	
	//Получение урона монстром
	public bool takeDamage(float damageAmount, float slowAmount){
		slowPower *= slowAmount;
		if((1-slowAmount)*Random.value > slowPower){
			frozen += 1-slowAmount;
			if(10*damageAmount*frozen*Random.value > health){
				return true;
			}
			if(!freezeEffect){
				freezeEffect = (Instantiate(freezeEffectSample.gameObject) as GameObject).GetComponent<OTSprite>();
	   			freezeEffect.position = me.position;
				freezeEffect.size = me.size;
			}
			slowPower = 0;
		}
		health -= damageAmount;
		
		HealthBar hb = GetComponent<HealthBar>();
        hb.health = (health);
        hb.healthMax = (healthMax);
        hb.resize();
		if(health < 0){
			return true;
		}else{
			return false;
		}
	}
	
	//Удаление полоски ХП монстра после его смерти
	void OnDestroy(){
		HealthBar hb = GetComponent<HealthBar>();
      	hb.deleteBar();
      	if(freezeEffect){
			GraphicClass gc = freezeEffect.GetComponent<GraphicClass>();
			gc.deleteEffect();
		}
	}
	
	//перемещение монстра
	public void moveMe(){
		shootDelay += Time.deltaTime*slowPower;
		if(shootDelay > shootCD){
			shootDelay = 0;
			shoot();
		}
		if(slowPower < 1.0){
			if(freezeEffect) {
	   			freezeEffect.position = me.position;
				freezeEffect.alpha = frozen+0.3f;
				frozen -= 0.01f;
			}else{
			slowPower += 0.002f;
			}
		}
		me.position = new Vector2(me.position.x - xMove*slowPower, me.position.y - yMove*slowPower);
	}
	
	//перемещения от взрывной волны
	public void blastMe(){
		float ryMove = sh.blastWaveSpeed*Mathf.Sin(angle);
		float rxMove = sh.blastWaveSpeed*Mathf.Cos(angle);
		
		me.position = new Vector2(me.position.x + rxMove, me.position.y + ryMove);
	}
	
	//Выстрел
	private void shoot(){
		angle = Mathf.Atan2(transform.position.y - Target.transform.position.y,
			transform.position.x - Target.transform.position.x);
		OTSprite nBullet = (Instantiate(bullet.gameObject) as GameObject).GetComponent<OTSprite>();
	           
	    nBullet.position = me.position;// + (gun.yVector * (gun.size.y / 2));
        nBullet.RotateTowards(TargetOT);

        nBullet.GetComponent<bulletClass>().shooter = me;
		
		gv.enemyBulletList.Add(nBullet);
	}
	
	void Update () {
	}
}
